import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { constantRouterMap, asyncRouterMap } from '@/config/router.config'

Vue.use(VueRouter)

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash',
    routes: [...constantRouterMap, ...asyncRouterMap]
})

// 路由前置守卫
// 判断是否登录状态
// 以后做权限管理相关
router.beforeEach((to, from, next) => {
    // console.log(storage.get('Access-Token'))
    if (storage.get(ACCESS_TOKEN)) {
        if (store.getters.token == '') {
            store.dispatch('GetInfo')
                .then(res => {
                    console.log(res)
                })
        }
        next()
    } else {
        next()
    }
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


export default router